package com.geeklei.videoplayer;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.media.AudioManager;
import android.media.MediaFormat;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;


import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Time: 2/17/22 11:24 AM
 * Author: leixun
 * Desc:
 */
public class GeekleiVideoView extends SurfaceView implements MediaController.MediaPlayerControl {
    private static final String TAG = "GeekleiVideoView";
    private static final int STATE_ERROR = -1;
    private static final int STATE_IDLE = 0;
    private static final int STATE_PREPARING = 1;
    private static final int STATE_PREPARED = 2;
    private static final int STATE_PLAYING = 3;
    private static final int STATE_PAUSED = 4;
    private static final int STATE_PLAYBACK_COMPLETED = 5;
    private Context mContext;
    private Uri mUri;
    private Map<String, String> mHeaders;
    private int mCurrentState;
    private int mTargetState;
    private SurfaceHolder mSurfaceHolder;
    private IjkMediaPlayer mMediaPlayer;
    private int mAudioSession;
    private int mVideoWidth;
    private int mVideoHeight;
    private int mSurfaceWidth;
    private int mSurfaceHeight;
    private MediaController mMediaController;
    private IjkMediaPlayer.OnCompletionListener mOnCompletionListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnPreparedListener mOnPreparedListener;
    private int mCurrentBufferPercentage;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener mOnErrorListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnInfoListener mOnInfoListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener;
    private int mSeekWhenPrepared;
    private boolean mCanPause;
    private boolean mCanSeekBack;
    private boolean mCanSeekForward;
    private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener;
    tv.danmaku.ijk.media.player.IMediaPlayer.OnVideoSizeChangedListener mSizeChangedListener;
    tv.danmaku.ijk.media.player.IMediaPlayer.OnPreparedListener mPreparedListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnCompletionListener mCompletionListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnInfoListener mInfoListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener mErrorListener;
    private tv.danmaku.ijk.media.player.IMediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener;
    private boolean mSurfaceFlag;
    SurfaceHolder.Callback mSHCallback;
    private boolean mHwDecFlag;
    private boolean mFastRenderFlag;
    private int mVolumeX;
    private int mNetTimeOutInSeconds;
    private boolean mCacheEnableFlag;
    private String mFileCacheDir;
    private String mFileSearchDir;
    private String mResourceToken;
    private long mTimeStamp;
    private boolean mEnableBackgroundPlay;

    public GeekleiVideoView(Context context) {
        this(context, (AttributeSet)null);
        this.mContext = context;
    }

    public GeekleiVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public GeekleiVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mCurrentState = 0;
        this.mTargetState = 0;
        this.mSurfaceHolder = null;
        this.mMediaPlayer = null;
        this.mOnAudioFocusChangeListener = null;
        this.mSizeChangedListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(tv.danmaku.ijk.media.player.IMediaPlayer mp, int width, int height, int sar_num, int sar_den) {
                GeekleiVideoView.this.mVideoWidth = mp.getVideoWidth();
                GeekleiVideoView.this.mVideoHeight = mp.getVideoHeight();
                if (GeekleiVideoView.this.mVideoWidth != 0 && GeekleiVideoView.this.mVideoHeight != 0) {
                    GeekleiVideoView.this.getHolder().setFixedSize(GeekleiVideoView.this.mVideoWidth, GeekleiVideoView.this.mVideoHeight);
                    GeekleiVideoView.this.requestLayout();
                }
            }
        };
        this.mPreparedListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnPreparedListener() {
            public void onPrepared(tv.danmaku.ijk.media.player.IMediaPlayer mp) {
                GeekleiVideoView.this.mCurrentState = 2;
                GeekleiVideoView.this.mCanPause = GeekleiVideoView.this.mCanSeekBack = GeekleiVideoView.this.mCanSeekForward = true;
                if (GeekleiVideoView.this.mOnPreparedListener != null) {
                    GeekleiVideoView.this.mOnPreparedListener.onPrepared(GeekleiVideoView.this.mMediaPlayer);
                }

                if (GeekleiVideoView.this.mMediaController != null) {
                    GeekleiVideoView.this.mMediaController.setEnabled(true);
                }

                GeekleiVideoView.this.mVideoWidth = mp.getVideoWidth();
                GeekleiVideoView.this.mVideoHeight = mp.getVideoHeight();
                int seekToPosition = GeekleiVideoView.this.mSeekWhenPrepared;
                if (seekToPosition != 0) {
                    GeekleiVideoView.this.seekTo(seekToPosition);
                }

                if (GeekleiVideoView.this.mVideoWidth != 0 && GeekleiVideoView.this.mVideoHeight != 0) {
                    Log.d("GeekleiVideoView", "video size: " + GeekleiVideoView.this.mVideoWidth + "/" + GeekleiVideoView.this.mVideoHeight);
                    Log.d("GeekleiVideoView", "surface size: " + GeekleiVideoView.this.mSurfaceWidth + "/" + GeekleiVideoView.this.mSurfaceHeight);
                    if (GeekleiVideoView.this.mSurfaceWidth > 0 && GeekleiVideoView.this.mSurfaceHeight > 0) {
                        if (GeekleiVideoView.this.mTargetState == 3) {
                            GeekleiVideoView.this.start();
                            if (GeekleiVideoView.this.mMediaController != null) {
                                GeekleiVideoView.this.mMediaController.show();
                            }
                        } else if (!GeekleiVideoView.this.isPlaying() && (seekToPosition != 0 || GeekleiVideoView.this.getCurrentPosition() > 0) && GeekleiVideoView.this.mMediaController != null) {
                            GeekleiVideoView.this.mMediaController.show(0);
                        }
                    }
                } else if (GeekleiVideoView.this.mTargetState == 3) {
                    GeekleiVideoView.this.start();
                }

            }
        };
        this.mCompletionListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnCompletionListener() {
            public void onCompletion(tv.danmaku.ijk.media.player.IMediaPlayer mp) {
                GeekleiVideoView.this.mCurrentState = 5;
                GeekleiVideoView.this.mTargetState = 5;
                if (GeekleiVideoView.this.mMediaController != null) {
                    GeekleiVideoView.this.mMediaController.hide();
                }

                if (GeekleiVideoView.this.mOnCompletionListener != null) {
                    GeekleiVideoView.this.mOnCompletionListener.onCompletion(GeekleiVideoView.this.mMediaPlayer);
                }

            }
        };
        this.mInfoListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnInfoListener() {
            public boolean onInfo(tv.danmaku.ijk.media.player.IMediaPlayer mp, int arg1, int arg2) {
                if (GeekleiVideoView.this.mOnInfoListener != null) {
                    GeekleiVideoView.this.mOnInfoListener.onInfo(mp, arg1, arg2);
                }

                return true;
            }
        };
        this.mErrorListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int framework_err, int impl_err) {
                Log.d("GeekleiVideoView", "Error: " + framework_err + "," + impl_err);
                GeekleiVideoView.this.mCurrentState = -1;
                GeekleiVideoView.this.mTargetState = -1;
                if (GeekleiVideoView.this.mMediaController != null) {
                    GeekleiVideoView.this.mMediaController.hide();
                }

                if (GeekleiVideoView.this.mOnErrorListener != null && GeekleiVideoView.this.mOnErrorListener.onError(GeekleiVideoView.this.mMediaPlayer, framework_err, impl_err)) {
                    return true;
                } else {
                    if (GeekleiVideoView.this.getWindowToken() != null) {
                        Resources r = GeekleiVideoView.this.mContext.getResources();
                        String msg;
                        if (framework_err == 200) {
                            msg = "Not Valid Playback!";
                        } else {
                            msg = "Unknown Error arg1 = " + framework_err + ", arg2 = " + impl_err;
                        }

                        AlertDialog.Builder builder = new AlertDialog.Builder(GeekleiVideoView.this.mContext);
                        builder.setMessage(msg).setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                GeekleiVideoView.this.release(true);
                            }
                        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                        (new AlertDialog.Builder(GeekleiVideoView.this.mContext)).setTitle(msg).setPositiveButton("Error", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                if (GeekleiVideoView.this.mOnCompletionListener != null) {
                                    GeekleiVideoView.this.mOnCompletionListener.onCompletion(GeekleiVideoView.this.mMediaPlayer);
                                }

                            }
                        }).setCancelable(false).show();
                    }

                    return true;
                }
            }
        };
        this.mBufferingUpdateListener = new tv.danmaku.ijk.media.player.IMediaPlayer.OnBufferingUpdateListener() {
            public void onBufferingUpdate(IMediaPlayer mp, int percent) {
                if (GeekleiVideoView.this.mOnBufferingUpdateListener != null) {
                    GeekleiVideoView.this.mOnBufferingUpdateListener.onBufferingUpdate(mp, percent);
                }

                GeekleiVideoView.this.mCurrentBufferPercentage = percent;
            }
        };
        this.mSurfaceFlag = false;
        this.mSHCallback = new SurfaceHolder.Callback() {
            public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
                Log.d("GeekleiVideoView", "surfaceChanged id = " + holder.getSurface() + ",mSurfaceWidth = " + GeekleiVideoView.this.mSurfaceWidth + ",mSurfaceHeight = " + GeekleiVideoView.this.mSurfaceHeight);
                GeekleiVideoView.this.mSurfaceWidth = w;
                GeekleiVideoView.this.mSurfaceHeight = h;
                boolean isValidState = GeekleiVideoView.this.mTargetState == 3;
                boolean hasValidSize = GeekleiVideoView.this.mVideoWidth > 0 && GeekleiVideoView.this.mVideoHeight > 0;
                if (GeekleiVideoView.this.mMediaPlayer != null && isValidState && hasValidSize) {
                    Log.d("GeekleiVideoView", "Set Media Player display holder!");
                    if (GeekleiVideoView.this.mSeekWhenPrepared != 0) {
                        GeekleiVideoView.this.seekTo(GeekleiVideoView.this.mSeekWhenPrepared);
                    }

                    Log.d("GeekleiVideoView", "Set Media Player start play!");
                    GeekleiVideoView.this.start();
                }

            }

            public void surfaceCreated(SurfaceHolder holder) {
                GeekleiVideoView.this.mSurfaceHolder = holder;
                Log.d("GeekleiVideoView", "surfaceCreated id = " + holder.getSurface());
                if (GeekleiVideoView.this.mSurfaceFlag && GeekleiVideoView.this.mEnableBackgroundPlay) {
                    if (GeekleiVideoView.this.mMediaPlayer != null) {
                        GeekleiVideoView.this.mMediaPlayer.setSurface(GeekleiVideoView.this.mSurfaceHolder.getSurface());
                    } else {
                        Log.d("GeekleiVideoView", "Media Player is null!");
                    }
                } else {
                    GeekleiVideoView.this.openVideo();
                    GeekleiVideoView.this.mSurfaceFlag = true;
                }

            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.d("GeekleiVideoView", "surfaceDestroyed id = " + holder.getSurface());
                GeekleiVideoView.this.mSurfaceHolder = null;
                if (GeekleiVideoView.this.mMediaController != null) {
                    GeekleiVideoView.this.mMediaController.hide();
                }

                if (!GeekleiVideoView.this.mEnableBackgroundPlay) {
                    GeekleiVideoView.this.release(true);
                }

            }
        };
        this.mHwDecFlag = false;
        this.mFastRenderFlag = false;
        this.mVolumeX = 1;
        this.mEnableBackgroundPlay = false;
        this.mContext = context;
        this.mVideoWidth = 0;
        this.mVideoHeight = 0;
        this.getHolder().addCallback(this.mSHCallback);
        this.getHolder().setType(3);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        this.requestFocus();
        this.mCurrentState = 0;
        this.mTargetState = 0;
        this.mHwDecFlag = false;
        this.mFastRenderFlag = false;
        this.resetCacheAndHlsPar();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(this.mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(this.mVideoHeight, heightMeasureSpec);
        Log.d("GeekleiVideoView", "onMeasure width = " + width + ",height = " + height + ",mVideoWidth = " + this.mVideoWidth + ",mVideoHeight = " + this.mVideoHeight);
        if (this.mVideoWidth > 0 && this.mVideoHeight > 0) {
            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
            if (widthSpecMode == 1073741824 && heightSpecMode == 1073741824) {
                width = widthSpecSize;
                height = heightSpecSize;
                if (this.mVideoWidth * heightSpecSize >= widthSpecSize * this.mVideoHeight && this.mVideoWidth * heightSpecSize > widthSpecSize * this.mVideoHeight) {
                }
            } else if (widthSpecMode == 1073741824) {
                width = widthSpecSize;
                height = widthSpecSize * this.mVideoHeight / this.mVideoWidth;
                if (heightSpecMode == -2147483648 && height > heightSpecSize) {
                    height = heightSpecSize;
                }
            } else if (heightSpecMode == 1073741824) {
                height = heightSpecSize;
                width = heightSpecSize * this.mVideoWidth / this.mVideoHeight;
                if (widthSpecMode == -2147483648 && width > widthSpecSize) {
                    width = widthSpecSize;
                }
            } else {
                width = this.mVideoWidth;
                height = this.mVideoHeight;
                if (heightSpecMode == -2147483648 && height > heightSpecSize) {
                    height = heightSpecSize;
                    width = heightSpecSize * this.mVideoWidth / this.mVideoHeight;
                }

                if (widthSpecMode == -2147483648 && width > widthSpecSize) {
                    width = widthSpecSize;
                    height = widthSpecSize * this.mVideoHeight / this.mVideoWidth;
                }
            }
        }

        Log.d("GeekleiVideoView", "onMeasure setMeasuredDimension width = " + width + ",height = " + height);
        this.setMeasuredDimension(width, height);
    }

    public CharSequence getAccessibilityClassName() {
        return GeekleiVideoView.class.getName();
    }

    public int resolveAdjustedSize(int desiredSize, int measureSpec) {
        return getDefaultSize(desiredSize, measureSpec);
    }

    public void setVideoPath(String path) {
        this.setVideoURI(Uri.parse(path));
    }

    public void setVideoURI(Uri uri) {
        this.setVideoURI(uri, (Map)null);
    }

    public void setVideoURI(Uri uri, Map<String, String> headers) {
        this.mUri = uri;
        this.mHeaders = headers;
        this.mSeekWhenPrepared = 0;
        this.openVideo();
        this.requestLayout();
        this.invalidate();
    }

    public void addSubtitleSource(InputStream is, MediaFormat format) {
        if (this.mMediaPlayer == null) {
        }

    }

    public void stopPlayback() {
        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.stop();
            this.mMediaPlayer.release();
            this.mMediaPlayer = null;
            this.mCurrentState = 0;
            this.mTargetState = 0;
            AudioManager am = (AudioManager)this.mContext.getSystemService(Context.AUDIO_SERVICE);
            am.abandonAudioFocus(this.mOnAudioFocusChangeListener);
        }

    }

    public void setOnAudioFocusChangeListener(AudioManager.OnAudioFocusChangeListener listener) {
        this.mOnAudioFocusChangeListener = listener;
    }

    private void openVideo() {
        if (this.mUri != null && this.mSurfaceHolder != null) {
            this.release(false);
            Log.d("GeekleiVideoView", "Create Media Player and set prepare!");
            AudioManager am = (AudioManager)this.mContext.getSystemService(Context.AUDIO_SERVICE);
            am.requestAudioFocus(this.mOnAudioFocusChangeListener, 3, 1);

            try {
                this.mMediaPlayer = new IjkMediaPlayer();
                Context context = this.getContext();
                this.mAudioSession = this.mMediaPlayer.getAudioSessionId();

                this.mMediaPlayer.setOnPreparedListener(this.mPreparedListener);
                this.mMediaPlayer.setOnVideoSizeChangedListener(this.mSizeChangedListener);
                this.mMediaPlayer.setOnCompletionListener(this.mCompletionListener);
                this.mMediaPlayer.setOnErrorListener(this.mErrorListener);
                this.mMediaPlayer.setOnInfoListener(this.mInfoListener);
                this.mMediaPlayer.setOnBufferingUpdateListener(this.mBufferingUpdateListener);
                this.mCurrentBufferPercentage = 0;
                

                this.mMediaPlayer.setDataSource(this.mContext, this.mUri, this.mHeaders);
                this.mMediaPlayer.setDisplay(this.mSurfaceHolder);
                this.mMediaPlayer.setAudioStreamType(3);
                this.mMediaPlayer.setScreenOnWhilePlaying(true);
                this.mMediaPlayer.prepareAsync();

                this.mCurrentState = 1;
                this.attachMediaController();
                return;
            } catch (IOException var9) {
                Log.w("GeekleiVideoView", "Unable to open content: " + this.mUri, var9);
                this.mCurrentState = -1;
                this.mTargetState = -1;
                this.mErrorListener.onError(this.mMediaPlayer, -8000, -1112);
                return;
            } catch (IllegalArgumentException var10) {
                Log.w("GeekleiVideoView", "Unable to open content: " + this.mUri, var10);
                this.mCurrentState = -1;
                this.mTargetState = -1;
                this.mErrorListener.onError(this.mMediaPlayer, -8000, -1112);
            } finally {
            }

        }
    }

    public IjkMediaPlayer getmMediaPlayer() {
        return mMediaPlayer;
    }

    public void setMediaController(MediaController controller) {
        if (this.mMediaController != null) {
            this.mMediaController.hide();
        }

        this.mMediaController = controller;
        this.attachMediaController();
    }

    private void attachMediaController() {
        if (this.mMediaPlayer != null && this.mMediaController != null) {
            this.mMediaController.setMediaPlayer(this);
            View anchorView = this.getParent() instanceof View ? (View)this.getParent() : this;
            this.mMediaController.setAnchorView((View)anchorView);
            this.mMediaController.setEnabled(this.isInPlaybackState());
        }

    }

    public void setOnPreparedListener(IMediaPlayer.OnPreparedListener l) {
        this.mOnPreparedListener = l;
    }

    public void setOnCompletionListener(IMediaPlayer.OnCompletionListener l) {
        this.mOnCompletionListener = l;
    }

    public void setOnBufferingUpdateListener(IMediaPlayer.OnBufferingUpdateListener listener) {
        this.mOnBufferingUpdateListener = listener;
    }

    public void setOnErrorListener(IMediaPlayer.OnErrorListener l) {
        this.mOnErrorListener = l;
    }

    public void setOnInfoListener(IMediaPlayer.OnInfoListener l) {
        this.mOnInfoListener = l;
    }

    private void release(boolean cleartargetstate) {
        if (this.mMediaPlayer != null) {
            this.mMediaPlayer.reset();
            this.mMediaPlayer.release();
            this.mMediaPlayer = null;
            this.mCurrentState = 0;
            if (cleartargetstate) {
                this.mTargetState = 0;
            }

            AudioManager am = (AudioManager)this.mContext.getSystemService(Context.AUDIO_SERVICE);
            am.abandonAudioFocus((AudioManager.OnAudioFocusChangeListener)null);
        }

    }

    public boolean onTouchEvent(MotionEvent ev) {
        if (this.isInPlaybackState() && this.mMediaController != null) {
            this.toggleMediaControlsVisiblity();
        }

        return false;
    }

    public boolean onTrackballEvent(MotionEvent ev) {
        if (this.isInPlaybackState() && this.mMediaController != null) {
            this.toggleMediaControlsVisiblity();
        }

        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean isKeyCodeSupported = keyCode != 4 && keyCode != 24 && keyCode != 25 && keyCode != 164 && keyCode != 82 && keyCode != 5 && keyCode != 6;
        if (this.isInPlaybackState() && isKeyCodeSupported && this.mMediaController != null) {
            if (keyCode == 79 || keyCode == 85) {
                if (this.mMediaPlayer.isPlaying()) {
                    this.pause();
                    this.mMediaController.show();
                } else {
                    this.start();
                    this.mMediaController.hide();
                }

                return true;
            }

            if (keyCode == 126) {
                if (!this.mMediaPlayer.isPlaying()) {
                    this.start();
                    this.mMediaController.hide();
                }

                return true;
            }

            if (keyCode == 86 || keyCode == 127) {
                if (this.mMediaPlayer.isPlaying()) {
                    this.pause();
                    this.mMediaController.show();
                }

                return true;
            }

            this.toggleMediaControlsVisiblity();
        }

        return super.onKeyDown(keyCode, event);
    }

    private void toggleMediaControlsVisiblity() {
        if (this.mMediaController != null) {
            if (this.mMediaController.isShowing()) {
                this.mMediaController.hide();
            } else {
                this.mMediaController.show();
            }
        }

    }

    public void start() {
        if (this.isInPlaybackState()) {
            this.mMediaPlayer.start();
            this.mCurrentState = 3;
        }

        this.mTargetState = 3;
    }

    public void pause() {
        if (this.isInPlaybackState() && this.mMediaPlayer.isPlaying()) {
            this.mMediaPlayer.pause();
            this.mCurrentState = 4;
        }

        this.mTargetState = 4;
    }

    public void suspend() {
        this.release(false);
    }

    public void resume() {
        this.openVideo();
    }

    public int getDuration() {
        return (int)(this.isInPlaybackState() ? this.mMediaPlayer.getDuration() : -1);
    }

    public int getCurrentPosition() {
        return (int)(this.isInPlaybackState() ? this.mMediaPlayer.getCurrentPosition() : 0);
    }

    public void seekTo(int msec) {
        if (this.isInPlaybackState()) {
            this.mMediaPlayer.seekTo(msec);
            this.mSeekWhenPrepared = 0;
        } else {
            this.mSeekWhenPrepared = msec;
        }

    }

    public boolean isPlaying() {
        return this.isInPlaybackState() && this.mMediaPlayer.isPlaying();
    }

    public int getBufferPercentage() {
        return this.mMediaPlayer != null ? this.mCurrentBufferPercentage : 0;
    }

    private boolean isInPlaybackState() {
        return this.mMediaPlayer != null && this.mCurrentState != -1 && this.mCurrentState != 0 && this.mCurrentState != 1;
    }

    private boolean isInIdleState() {
        return this.mMediaPlayer != null && this.mCurrentState != 0;
    }

    public boolean canPause() {
        return this.mCanPause;
    }

    public boolean canSeekBackward() {
        return this.mCanSeekBack;
    }

    public boolean canSeekForward() {
        return this.mCanSeekForward;
    }

    public int getAudioSessionId() {
        if (this.mAudioSession == 0) {
            IjkMediaPlayer foo = new IjkMediaPlayer();
            this.mAudioSession = foo.getAudioSessionId();
            foo.release();
        }

        return this.mAudioSession;
    }

    public float getSpeed() {
        return this.isInPlaybackState() ? this.mMediaPlayer.getSpeed(1.0f) : 1.0F;
    }

    public void setSpeed(float speed) {
        if (this.isInPlaybackState()) {
            this.mMediaPlayer.setSpeed(speed);
        }

    }

    public void setHwDecEnable(boolean flag, boolean fastRenderFlag) {
        this.mHwDecFlag = flag;
        if (!flag) {
            this.mFastRenderFlag = false;
        } else {
            this.mFastRenderFlag = fastRenderFlag;
        }

    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.d("GeekleiVideoView", "layout: left = " + left + ",top = " + top + ",right = " + right + ",bottom" + bottom);
        super.onLayout(changed, left, top, right, bottom);
    }

    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    private void resetCacheAndHlsPar() {
        this.mVolumeX = 1;
        this.mNetTimeOutInSeconds = -1;
        this.mCacheEnableFlag = false;
        this.mFileCacheDir = null;
        this.mFileSearchDir = null;
        this.mResourceToken = null;
        this.mTimeStamp = -1L;
        this.mSurfaceFlag = false;
    }

    public void setAudioVolumeMagX(int volumeX) {
        this.mVolumeX = volumeX;
    }

    public void setNetTimeout(int seconds) {
        this.mNetTimeOutInSeconds = seconds;
    }

    public void setCacheEnable(boolean flag) {
        this.mCacheEnableFlag = flag;
    }

    public void setCacheDir(String dir) {
        this.mFileCacheDir = dir;
    }

    public void setSearchDir(String dir) {
        this.mFileSearchDir = dir;
    }

    public void setToken(String token) {
        this.mResourceToken = token;
    }

    public void setTimestamp(long timestamp) {
        this.mTimeStamp = timestamp;
    }

    public void enableBackgroundPlay(boolean enbale) {
        this.mEnableBackgroundPlay = enbale;
    }

}